package com.cctv.fmaed.module.redis.memory.pojo.po;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 投屏状态PO
 *
 * @author heyingcheng
 * @since 2023/8/29
 */
public class PsStatePO extends LinkedHashMap<String, String> {

    public static final String HASH_KEY_GUID = "guid";
    public static final String HASH_KEY_USER_ID = "user_id";
    public static final String HASH_KEY_ITEM_ID = "item_id";
    public static final String HASH_KEY_STATE = "state";
    public static final String HASH_KEY_PROGRESS = "progress";
    public static final String HASH_KEY_DURATION = "duration";
    public static final String HASH_KEY_TIMESTAMP = "timestamp";

    public PsStatePO(String guid, String userId, String itemId, String state, Long progress, Long duration, Long timestamp) {
        put(HASH_KEY_GUID, guid);
        put(HASH_KEY_USER_ID, userId);
        put(HASH_KEY_ITEM_ID, itemId);
        put(HASH_KEY_STATE, state);

        if (Objects.nonNull(progress)) {
            put(HASH_KEY_PROGRESS, String.valueOf(progress));
        }

        if (Objects.nonNull(duration)) {
            put(HASH_KEY_DURATION, String.valueOf(duration));
        }

        if (Objects.nonNull(timestamp)) {
            put(HASH_KEY_TIMESTAMP, String.valueOf(timestamp));
        }
    }

    public String getGuid() {
        return get(HASH_KEY_GUID);
    }

    public String getUserId() {
        return get(HASH_KEY_USER_ID);
    }

    public String getItemId() {
        return get(HASH_KEY_ITEM_ID);
    }

    public String getState() {
        return get(HASH_KEY_STATE);
    }

    private Optional<Long> getLong(String hashKeyProgress) {
        String progress = get(hashKeyProgress);
        if (Objects.nonNull(progress)) {
            return Optional.of(Long.parseLong(progress));
        } else {
            return Optional.empty();
        }
    }

    public Optional<Long> getProgress() {
        return getLong(HASH_KEY_PROGRESS);
    }

    public Optional<Long> getDuration() {
        return getLong(HASH_KEY_DURATION);
    }

    public Optional<Long> getTimestamp() {
        return getLong(HASH_KEY_TIMESTAMP);
    }

    public static void opData(Long data, String key, Map<String, String> entries) {
        if (Objects.nonNull(data)) {
            entries.put(key, String.valueOf(data));
        } else {
            entries.put(key, "-1");
        }
    }

}
